﻿using AdminStystem.Domain;
using AdminSytem.Application.Dto;
using AdminSytem.Repositories;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminSytem.Application.Cultivations
{
    public class CultivationServices : ICultivationServices
    {
        private readonly IRepository<Cultivation> repository;
        private readonly IRepository<Enterprise> enterpriserepository;
        private readonly IRepository<Provinces> provincesrepository;
        private readonly IRepository<Cities> citiesrepository;
        private readonly IRepository<Areas> areasrepository;
       
        private readonly IMapper mapper;

        public CultivationServices(IRepository<Cultivation> repository, IMapper mapper, IRepository<Enterprise> enterpriserepository, IRepository<Provinces> provincesrepository, IRepository<Cities> citiesrepository, IRepository<Areas> areasrepository)
        {
            this.repository = repository;
            this.mapper = mapper;
            this.enterpriserepository = enterpriserepository;
            this.provincesrepository= provincesrepository;
            this.citiesrepository= citiesrepository;
            this.areasrepository= areasrepository;
        }

        /// <summary>
        /// 添加养殖信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ResultDto<int>> AddCultivation(CultivationDto dto)
        {
            ResultDto<int> result = new ResultDto<int>();
            result.StatusCode = StatusCode.Success;
            result.Message = "添加成功";
            var entity = mapper.Map<Cultivation>(dto);
            await repository.InsertAsync(entity);
            return result;

        }      
        /// <summary>
        /// 获取养殖信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<Cultivation>> GetCultivations(int id)
        {
            ResultDto<Cultivation> result = new ResultDto<Cultivation>();
            result.StatusCode = StatusCode.Success;
            result.Message = "查询成功";
            var list = await repository.QueryFirstAsync(a => a.GoodsId == id);
            result.Data = list;
            return result;
        }
        /// <summary>
        /// 添加企业信息
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        public async Task<ResultDto<int>> AddEnterprise(EnterpriseDto dto)
        {
            ResultDto<int> result = new ResultDto<int>();
            result.StatusCode = StatusCode.Success;
            result.Message = "添加成功";
            var entity = mapper.Map<Enterprise>(dto);
            await enterpriserepository.InsertAsync(entity);
            return result;
        }
        /// <summary>
        /// 获取企业信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ResultDto<Enterprise>> GetEnterprise(int id)
        {
            ResultDto<Enterprise> result = new ResultDto<Enterprise>();
            result.StatusCode = StatusCode.Success;
            result.Message = "查询成功";
            var list = await enterpriserepository.QueryFirstAsync(a => a.GoodsId == id);
            result.Data = list;
            return result;
        }
        public async Task<ResultDto<List<Provinces>>> GetProvinces()
        {
            ResultDto<List<Provinces>> result = new ResultDto<List<Provinces>>();
            result.StatusCode = StatusCode.Success;
            result.Message = "查询成功";
            var list = await provincesrepository.QueryAsync();
            result.Data = list;
            return result;
        }
        public async Task<ResultDto<List<Cities>>> GetCities(string ProvinceId)
        {
            ResultDto<List<Cities>> result = new ResultDto<List<Cities>>();
            result.StatusCode = StatusCode.Success;
            result.Message = "查询成功";
            var list = await citiesrepository.QueryAsync(x=>x.ProvinceId==ProvinceId);
            result.Data = list;
            return result;
        }
        public async Task<ResultDto<List<Areas>>> GetAreas(string CityId)
        {
            ResultDto<List<Areas>> result = new ResultDto<List<Areas>>();
            result.StatusCode = StatusCode.Success;
            result.Message = "查询成功";
            var list = await areasrepository.QueryAsync(x => x.CityId == CityId);
            result.Data = list;
            return result;
        }
    }
}
